# Analyses for Hafner-Burton, Emilie, Brad L. LeVeck, and David G. Victor
# How Activists Perceive the Utility of International Law 

# This file contains all code necessary to reproduce the analyses 
# in the main article and the Supplementary Information appendix. The only 
# exception is the regression analysis in the Supplementary Information Appendix
# the regressions were done in Stata, and the code to complete them is in the 
# regressions.do file.

# Libraries
library(foreign)
library(Hmisc)
library(magrittr)
library(plyr)
library(dplyr)
library(tidyr)
library(broom)
library(ggplot2)
library(reshape2)

# load de identified data
hr.env.data <- read.dta("hr.env.data.dta")
hr.env.data <- tbl_df(hr.env.data)

# Table 1 Demographics (Main Paper)
table1demographics <- group_by(hr.env.data, policy_area) %>% 
  summarise(prop_econ = mean(econ, na.rm=T), 
            legal = mean(law, na.rm=T),
            female = mean(female, na.rm=T),
            income= median(income, na.rm=T),
            ideology = median(ideology, na.rm=T),
            ngo_budget = median(ngo_budget, na.rm=T),
            international = mean(international, na.rm=T)
  )
table1demographics


# Figure 1 The effect of removing a particular agreement
law.data <- filter(hr.env.data, !is.na(d_main)) %>% select(subject_id, policy_area, 11:24) 
melted.law.data <- melt(law.data,id.vars=c("subject_id", "policy_area")) %>%
  separate(col="variable", into = c("strategy", "binding"))
melted.law.data$binding <- plyr::mapvalues(melted.law.data$binding, 
                                           from = c("b", "nb"), 
                                           to = c("Binding", "Non-Binding"))
melted.law.data$policy_area <- plyr::mapvalues(melted.law.data$policy_area, 
                                           from = c("environment", "human.rights"), 
                                           to = c("Environment", "Human Rights"))
melted.law.data$strategy <- plyr::mapvalues(melted.law.data$strategy, 
                                               from = c("main", "litigation", "accountability", "leverage", "symbolic", "information", "capacity"), 
                                               to = c("Main", "Litigation", "Accountability", "Leverage", "Symbolic", "Information", "Capacity"))
melted.law.data$strategy <- factor(melted.law.data$strategy, 
                                    levels = c("Main", "Litigation", "Accountability", "Leverage", "Symbolic", "Information", "Capacity"))
melted.law.data$strategy <- factor(melted.law.data$strategy, levels = levels (melted.law.data$strategy)[7:1])

sem <- function(x) sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))
lower <- function(x) mean(x, na.rm=T) + sem(x) * -1.96
upper <- function(x) mean(x, na.rm=T) + sem(x) * 1.96

law.summary2 <- group_by(melted.law.data, binding, policy_area, strategy) %>% 
  summarise(mean_val = mean(value, na.rm=T), lower_ci = lower(value), upper_ci = upper(value))

dodge <- position_dodge(width=-0.5)  
hl.plot2 <- ggplot(data=subset(law.summary2), aes(y=mean_val, x=strategy, color=policy_area))
hl.plot2 <- hl.plot2 + geom_point(aes(), position=dodge, size=4)+geom_errorbar( aes(ymin=lower_ci, ymax=upper_ci), position=dodge, width=0)
#hl.plot2 <- hl.plot2 + geom_hline(yintercept=0, linetype="dashed", color="darkgrey")
hl.plot2 <- hl.plot2 + facet_grid(facets=.~binding,)
hl.plot2 <- hl.plot2 + theme_bw()+theme(
  axis.title.x=element_text(size=18, vjust= -.5, face="bold"), 
  axis.title.y=element_text(size=18, vjust=-0, face="bold"),
  axis.text.y=element_text(size=16, vjust=.5, 
                           face=c('plain', 'plain', 'plain','plain','plain','plain','bold'), 
                           color=c("grey33", "grey33","grey33","grey33","grey33","grey33","black")) , 
  axis.text.x=element_text(size=16, face="bold"), 
  legend.position=c(.62,.8), legend.justification=c(1,1), 
  legend.title = element_text(size=14), 
  legend.text = element_text(size = 13), 
  legend.background = element_rect(fill="white", size=.2, color="grey"), 
  strip.text.x = element_text(size=18, face="bold"), 
  strip.background = element_rect( fill="white", color="white")
)
hl.plot2 <- hl.plot2 +coord_flip() + scale_color_discrete(name="Policy Area", labels=c("Human Rights", "Environment"))+ylab(label="Change in NGO Influence After Removing an Agreement")+xlab(label="")
hl.plot2
ggsave(plot=hl.plot2, filename="Figure 1 - The effect of removing a particular agreement.pdf", width=12, height=6)

# Figure 2: Differences in the Effect of Legal Form. Lines represent 95% CIs

fig2_data <- select(hr.env.data,subject_id, policy_area, starts_with("d_")) %>%
  dplyr::rename(Main=d_main, 
                Litigation=d_litigation, 
                Accountability=d_accountability, 
                Leverage=d_leverage, 
                Symbolic=d_symbolic, 
                Information=d_information, 
                Capacity=d_capacity) %>%
  melt(id.vars = c("subject_id", "policy_area")) %>%
  group_by(policy_area, variable) %>%
  summarise( mean_val=mean(-value, na.rm=T), lower_ci = lower(-value), upper_ci = upper(-value))

fig2_data$policy_area <- mapvalues(fig2_data$policy_area, 
                                   from = c("environment", "human.rights"), 
                                   to = c("Environment", "Human Rights"))

fig2_data$variable <- factor(fig2_data$variable, levels=levels(fig2_data$variable)[7:1])


diffsBar <- ggplot(fig2_data, aes(y=mean_val, x=variable, fill=policy_area)) 
diffsBar <- diffsBar + geom_bar(aes(),stat="identity", show_guide=T, position=dodged) 
diffsBar <- diffsBar + geom_errorbar(aes(ymin=lower_ci, ymax=upper_ci, width=.1), position=dodged,  size=.7, show_guide=F) 
diffsBar <- diffsBar + theme(axis.title.x=element_text(size=20, vjust= -.5, face="bold"), 
                             axis.title.y=element_text(size=20, vjust=-0, face="bold"), 
                             axis.text.y=element_text(size=16, vjust=.5, face="bold", 
                                                      color=c("grey33", "grey33","grey33","grey33","grey33","grey33","black")) , 
                             axis.text.x=element_text(size=16, face="bold"), 
                             panel.grid.major=element_blank(), 
                             panel.grid.minor=element_blank(), 
                             panel.background=element_blank(), 
                             plot.margin=unit(c(1,1,1,1),"cm"), 
                             axis.line=element_line(), 
                             legend.title= element_text(size=18, face="bold"), 
                             legend.text=element_text(size=16, face="bold"), 
                             legend.position=c(1.00,.9), legend.justification=c(1,1)) 
diffsBar <- diffsBar + scale_y_continuous(expand = c(0, 0), limits=c(-4.9,15)) 
diffsBar <- diffsBar + guides(fill=guide_legend(title="Policy Area")) 
diffsBar <- diffsBar + ylab("Binding - Non-Binding")+ xlab(" ") + coord_flip() 
diffsBar <- diffsBar + facet_grid(facets=.~policy_area)
diffsBar <- diffsBar + theme(strip.text.x = element_text(size=18, face="bold"), 
                             strip.background = element_rect( fill="white", color="white")) 
diffsBar <- diffsBar + guides(fill=FALSE)

diffsBar

ggsave(plot=diffsBar, filename="Figure 2 - Differences in the Effect of Legal Form.pdf", width=10, height=6)



# Figure 3 manipulation check
manipulation.check.data <- read.dta("manipulation.check.data.dta")
melted.manipulation.check.data <- melt(data=manipulation.check.data, id.vars=c("subject_id", "policy_area"))
melted.manipulation.check.data$variable <- factor(melted.manipulation.check.data$variable, levels=c("Scope", "Difficulty", "Obligation", "Precision", "Legal_Force"), labels=c("Scope", "Difficulty", "Obligation", "Precision", "Legal Force"))
manipulation.check.summary <- ddply(.data=melted.manipulation.check.data, .variables=.(policy_area, variable),.fun= function(x) smean.cl.normal(x$value, conf.int=.95))

dodged <- position_dodge(width= -0.5)  
mc.plot <- ggplot(data=manipulation.check.summary, aes(y=Mean, x=variable, color=policy_area))
mc.plot <- mc.plot + geom_point(aes(), size=6, position=dodged)+geom_errorbar( aes(ymin=Lower, ymax=Upper), width=0, position=dodged)+ylab(label="Difference between binding and non-binding agreement")+xlab(label="")+theme_bw()
mc.plot <- mc.plot + scale_color_discrete(name="Policy Area", labels=c("Human Rights", "Environment"))
mc.plot <- mc.plot + scale_y_continuous(limits=c(-12,50))
mc.plot <- mc.plot + theme(
  axis.title.y=element_text(size=16, vjust= -.5, face="bold"), 
  axis.title.x=element_text(size=18, vjust=-0, face="bold"),
  axis.text.y=element_text(size=16, vjust=.5, face=c('plain', 'plain', 'plain','plain','plain','plain','bold')), 
  axis.text.x=element_text(size=16, face="bold"), 
  legend.position=c(.85,.4), legend.justification=c(1,1), legend.title = element_text(size=14), 
  legend.text = element_text(size = 13), legend.background = element_rect(fill="white", size=.2, color="grey"))
mc.plot <- mc.plot + coord_flip()
mc.plot
ggsave(plot=mc.plot, filename="Figure 3 - Manipulation Check.pdf", width=8.5, height=6)


# appendix table 1
d_main_test <- t.test(d_main~policy_area, data=hr.env.data)
d_litigation_test <- t.test(d_litigation~policy_area, data=hr.env.data)
d_accountability_test <- t.test(d_accountability~policy_area, data=hr.env.data)
d_leverage_test <- t.test(d_leverage~policy_area, data=hr.env.data)
d_symbolic_test <- t.test(d_symbolic~policy_area, data=hr.env.data)
d_information_test <- t.test(d_information~policy_area, data=hr.env.data)
d_capacity_test <- t.test(d_capacity~policy_area, data=hr.env.data)

policy_area_ttests <- rbind(
  tidy(d_main_test),
  tidy(d_litigation_test),
  tidy(d_accountability_test),
  tidy(d_leverage_test),
  tidy(d_symbolic_test),
  tidy(d_information_test),
  tidy(d_capacity_test)
) 
row.names(policy_area_ttests) <-  c("Main", 
                      "Litigation", 
                      "Accountability", 
                      "Leverage", 
                      "Symbolic", 
                      "Information",
                      "Capacity")

round(policy_area_ttests, 2)

# See stat script for regressions
